class Time {
  constructor(options) {
    this.options = options;

    this.date = '';
    this.time = '';
    this.upDate();
  }


  upDate(t) {
    if(t){
      this.date = new Date(t);
    }else{
      this.date = new Date();
    }
    // 年 月 日 时 分 秒
    this.time = {
      year: this.date.getFullYear(),
      month: this.date.getMonth() + 1,
      day: this.date.getDate(),
      hour: this.date.getHours(),
      minute: this.date.getMinutes(),
      second: this.date.getSeconds(),
    };
  }

  yearMonthDayFormat(type = 'time') {
    // 1735056000000
    // this.upDate();
    if('tiem'){
      return new Date(this.time.year, this.time.month, this.time.day).getTime()
    }else if('date'){
      return `${this.time.year}-${this.time.month}-${this.time.day}`
    }
  }


  msToTime(s) {
    var ms = s % 1000;
    s = (s - ms) / 1000;
    var secs = s % 60;
    s = (s - secs) / 60;
    var mins = s % 60;
    var hrs = (s - mins) / 60;
    return {
      hrs, mins , secs , ms
    };
  }

  msToDate(s) {
    this.date = new Date(s);
    // 年 月 日 时 分 秒
    return {
      year: this.date.getFullYear(),
      month: this.date.getMonth() + 1,
      day: this.date.getDate(),
      hour: this.date.getHours(),
      minute: this.date.getMinutes(),
      second: this.date.getSeconds(),
    };
  }

  an(cd) {
    requestAnimationFrame(() => {
      let oldTime = this.yearMonthDayFormat();
      this.upDate();
      let newTime = this.yearMonthDayFormat();
      if( oldTime !==  newTime){
        cd();
      }
      this.an(cd);
    });
  }

}

export {
  Time
};